package com.suning.sawp.web.controller;

import java.security.Principal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.constants.PreChargeErrorConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.precharge.ChargeCashOrderInfo;
import com.suning.sawp.dto.precharge.ChargeOrderRtn;
import com.suning.sawp.dto.precharge.ClertChargeRecord;
import com.suning.sawp.dto.precharge.CommonRtn;
import com.suning.sawp.dto.precharge.PreChargeAct;
import com.suning.sawp.dto.precharge.PreChargeDetailRtn;
import com.suning.sawp.dto.precharge.PrechargeRemain;
import com.suning.sawp.dto.precharge.StoreChargeRecord;
import com.suning.sawp.intf.precharge.PreChargeService;
import com.suning.sawp.service.impl.precharge.PosSendSMSService;
import com.suning.sawp.service.util.DateUtils;
import com.suning.sawp.web.exception.MyGsonView;

/**
 * 
 * 〈一句话功能简述〉<br> 
 * 〈功能详细描述〉
 *
 * @author Wuxiao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/precharge")
public class PreChargeController extends DjBaseController{

    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);

    @Autowired
    PreChargeService            preChargeService;
    
    @Autowired
    PosSendSMSService posSendSMSService;
    
    /**
     * 
     * 功能描述: 查询门店充值规则<br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryPreCharge.do")
    public MyGsonView queryPreCharge(String storeCode) {
        MyGsonView gsonView = createGsonView();
        ReturnMsg<PreChargeDetailRtn> ret = new ReturnMsg<PreChargeDetailRtn>();
        try {
            ret = preChargeService.queryStoreActDetail(storeCode);
        } catch (Exception e) {
            ret.setRetFlag(ReturnMsg.FAIL);
            ret.setErrorCode(PreChargeErrorConstants.PRECHARGE_ERR_E999);
            ret.setErrorMessage(PreChargeErrorConstants.PRECHARGE_ERR_MAP
                    .get(PreChargeErrorConstants.PRECHARGE_ERR_E999));
            LOGGER.error("查询充值活动信息异常:",e);
        }
        gsonView.addStaticAttribute("ret", ret);
        gsonView.setJsonObjectName("ret");
        return gsonView;
    }
   
    /**
     * 
     * 功能描述: 现金充值接口<br>
     * 〈功能详细描述〉
     *
     * @param telNo 手机号
     * @param storeCode 门店编码
     * @param ruleCode 规则编码
     * @param amount 充值金额
     * @param serialNo 流水号
     * @param sign 签名
     * @param principal
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/payOrder.do")
    public MyGsonView payOrder(String telNo,String storeCode, String ruleCode,
             String amount, String serialNo,String sign,Principal principal) {
        MyGsonView gsonView = createGsonView();
        ReturnMsg<ChargeOrderRtn> ret = new ReturnMsg<ChargeOrderRtn>();
        try {
            ret = preChargeService.payChargeOrder(telNo, storeCode, principal.getName(), ruleCode, amount, serialNo,sign);
        } catch (Exception e) {
            ret.setRetFlag(ReturnMsg.FAIL);
            ret.setErrorCode(PreChargeErrorConstants.PRECHARGE_ERR_E999);
            ret.setErrorMessage(PreChargeErrorConstants.PRECHARGE_ERR_MAP
                    .get(PreChargeErrorConstants.PRECHARGE_ERR_E999));
            LOGGER.error("现金充值异常--手机号:"+telNo+",门店编码:"+storeCode+",规则编码:"+ruleCode+",充值金额:"+amount+"客户端流水号:"+serialNo,e);
        }
        gsonView.addStaticAttribute("ret", ret);
        gsonView.setJsonObjectName("ret");
        return gsonView;
    }
    
    
    /**
     * 
     * 功能描述: <br>
     *   查询现金充值记录
     *
     * @param chargeDate
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryOder.do")
    public MyGsonView queryOrder(String chargeDate, Principal principal) {
        ReturnMsg<List<ChargeCashOrderInfo>> result = new ReturnMsg<List<ChargeCashOrderInfo>>();
        MyGsonView gsonView = createGsonView();
        // 校验日期
        Date charDate = DateUtils.getDateByStr(chargeDate);
        if (null == charDate) {
            result.setRetFlag(ReturnMsg.FAIL);
            result.setError("SAWP001");
            result.setErrorMessage("日期格式非法！chargeDate: " + chargeDate);
        } else {
            // 转换日期格式为yyyyMMdd
            result = preChargeService.queryOrderList(
                    DateUtils.getDateStrByFormat(charDate, DateUtils.YYYYMMDD_PATTERN), principal.getName());
        }

        gsonView.addStaticAttribute("ret", result);
        gsonView.setJsonObjectName("ret");
        return gsonView;
   }
    
    
    /**
     * 
     * 功能描述: 校验充值时间<br>
     * 〈功能详细描述〉
     *
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/checkTime.do")
    public MyGsonView validChargeTime(){
        MyGsonView gsonView = createGsonView();
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        ret.setRetFlag(ReturnMsg.SUCCESS);
        if(preChargeService.validChargeTime()){
            ret.setErrorCode(PreChargeErrorConstants.VALID_CHARGE_TIME_Y);
        }else{
            ret.setErrorCode(PreChargeErrorConstants.VALID_CHARGE_TIME_N);
        }
        gsonView.addStaticAttribute("ret", ret);
        gsonView.setJsonObjectName("ret");
        return gsonView;
    }
    
    /**
     * 
     * 功能描述: 校验店+用户密码<br>
     * 〈功能详细描述〉
     *
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/validUser.do")
    public MyGsonView validUser(String pwd,Principal principal){
        MyGsonView gsonView = createGsonView();
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        ret.setRetFlag(ReturnMsg.SUCCESS);
        if(preChargeService.validUserPwd(principal.getName(), pwd)){
            ret.setErrorCode(PreChargeErrorConstants.VALID_PWD_Y);
        }else{
            ret.setErrorCode(PreChargeErrorConstants.VALID_PWD_N);
        }
        gsonView.addStaticAttribute("ret", ret);
        gsonView.setJsonObjectName("ret");
        return gsonView;
    }
   
    /**
     * 
     * 功能描述: 重新发送充值成功短信<br>
     * 〈功能详细描述〉
     *
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/sendMessage.do")
    public MyGsonView sendMessage(String mobile, String orderNo) {
        MyGsonView gsonView = createGsonView();
    	@SuppressWarnings("rawtypes")
        ReturnMsg returnMsg = new ReturnMsg();
    	try{
    		returnMsg = preChargeService.sendMessage(orderNo);
    	}catch (Exception e){
    		LOGGER.warn("sendMessage failed,mobile="+mobile+"orderNo="+orderNo+",error="+e);
    		returnMsg.setRetFlag(ReturnMsg.FAIL);
    		returnMsg.setErrorCode(PreChargeErrorConstants.PRECHARGE_ERR_E999);
    		returnMsg.setErrorMessage(PreChargeErrorConstants.PRECHARGE_ERR_MAP
                     .get(PreChargeErrorConstants.PRECHARGE_ERR_E999));
    	}
    	 gsonView.addStaticAttribute("ret", returnMsg);
         gsonView.setJsonObjectName("ret");
         return gsonView;
    }
    
    @RequestMapping("/posSendSMS.htm")
    public void posSendSMS(HttpServletRequest request,HttpServletResponse response){
        try{
            posSendSMSService.handleHttpService(request, response);
        }catch(Exception e){
            LOGGER.error("POS调用重发短信接口异常:",e);
        }
    }
    
    /**
     * 
     * 功能描述: 查询预充值剩余额度<br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @param couponAmount
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryRemain.do")
    public MyGsonView queryOrder(String storeCode,String couponAmount){
        MyGsonView gsonView = createGsonView();
        ReturnMsg<PrechargeRemain> ret = new ReturnMsg<PrechargeRemain>();
        try {
            ret = preChargeService.queryPrechargeRemain(storeCode, couponAmount);
        } catch (Exception e) {
            ret.setRetFlag(ReturnMsg.FAIL);
            ret.setErrorCode(PreChargeErrorConstants.PRECHARGE_ERR_E999);
            ret.setErrorMessage(PreChargeErrorConstants.PRECHARGE_ERR_MAP
                    .get(PreChargeErrorConstants.PRECHARGE_ERR_E999));
            LOGGER.error("查询预充值剩余额度异常",e);
        }
        gsonView.addStaticAttribute("ret", ret);
        gsonView.setJsonObjectName("ret");
        return gsonView;
    }
    /**
     * 
     * 功能描述: 查询店员充值记录<br>
     * 〈功能详细描述〉
     *
     * @param ruleCode
     * @param storeCode
     * @param pageNo
     * @param pageSize
     * @param principal
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryStaffRecord.do")
    public MyGsonView queryStaffRecord(String ruleCode,String storeCode,Integer pageNo,Integer pageSize,Principal principal){
        ReturnMsg<ClertChargeRecord> ret = new ReturnMsg<ClertChargeRecord>();
        try{
            ret = preChargeService.queryClerkChargeRecord(ruleCode, storeCode, pageNo, pageSize, principal.getName());
        }catch (Exception e) {
            ret.setError(PreChargeErrorConstants.PRECHARGE_ERR_E999, PreChargeErrorConstants.PRECHARGE_ERR_MAP);
            LOGGER.error("查询店员充值记录异常",e);
        }
        return setGsonView(ret);
    }
    /**
     * 
     * 功能描述: 查询券未过期的充值活动<br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryActList.do")
    public MyGsonView queryActList(String storeCode){
        ReturnMsg<List<PreChargeAct>> ret = new ReturnMsg<List<PreChargeAct>>();
        try{
            ret = preChargeService.queryPrechargeActList(storeCode);
        }catch(Exception e){
            ret.setError(PreChargeErrorConstants.PRECHARGE_ERR_E999, PreChargeErrorConstants.PRECHARGE_ERR_MAP);
            LOGGER.error("查询券未过期的充值活动异常",e);
        }
        return setGsonView(ret);
    }
    /**
     * 
     * 功能描述: 店长查询门店预充值统计<br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @param sortType
     * @param sortColumn
     * @param pageSize
     * @param pageNo
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryManagerRecord.do")
    public MyGsonView queryStoreStatis(String storeCode,Integer sortType,Integer sortColumn,Integer pageSize,Integer pageNo){
        ReturnMsg<StoreChargeRecord> ret = new ReturnMsg<StoreChargeRecord>();
        try{
            ret = preChargeService.queryStoreChargeRecord(sortColumn, sortType, storeCode, pageNo, pageSize);
        }catch(Exception e){
            ret.setError(PreChargeErrorConstants.PRECHARGE_ERR_E999, PreChargeErrorConstants.PRECHARGE_ERR_MAP);
            LOGGER.error("店长查询门店预充值统计异常",e);
        }
        return setGsonView(ret);
    }
    /**
     * 
     * 功能描述: 店长上下架活动<br>
     * 〈功能详细描述〉
     *
     * @param operateType
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/operateAct.do")
    public MyGsonView updateActStatus(Integer operateType,String storeCode){
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        try{
            ret = preChargeService.updateActStatus(storeCode, operateType);
        }catch (Exception e) {
            ret.setError(PreChargeErrorConstants.PRECHARGE_ERR_E999, PreChargeErrorConstants.PRECHARGE_ERR_MAP);
            LOGGER.error("预充值上下架活动异常",e);
        }
        return setGsonView(ret);
    }
}
